import Apicloud from '@/lib/core/Apicloud';

/**
 * 拼多多工具
 */
export default class PinDuoDuoUtils {
    private static instance: PinDuoDuoUtils;

    public static getInstance() {
        if (!this.instance) {
            this.instance = new PinDuoDuoUtils();
        }
        return this.instance;
    }


    private constructor() {

    }
    /**
     * 判断是否安装
     */
    public installed() {
        const bundleId = api.systemType == 'ios' ? 'pinduoduo://' : 'com.xunmeng.pinduoduo';
        const installed = api.appInstalled({
            sync: true,
            appBundle: bundleId
        });
        return installed;
    }

    /**
     * 打开链接
     * @param params 
     */
    public openByUrl(params: { url: string }) {
        const url = params.url.replace(/https?:\/\//g, "").replace('mobile.yangkeduo.com', 'com.xunmeng.pinduoduo');
        const byUrl = `pinduoduo://${url}`;
        if (this.installed()) {
            api.openApp({
                iosUrl: byUrl,
                uri: byUrl
            });
        } else {
            Apicloud.getInstance().openH5NavtiveTitleUrl(params.url, {
                title: '拼多多'
            });
        }

    }


}
